package com.ellecity06.common.widgets.dialog

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import com.ellecity06.common.R
import com.ellecity06.common.base.BaseDialog
import com.ellecity06.common.databinding.DialogAlertBinding

/**
 * @author ellecity06
 * @time 2024/10/28 15:11
 * @des
 */
@JvmOverloads
fun Activity.showAlert(
    title: String = "提示",
    content: String? = null,
    cancelText: String = "取消",
    confirmText: String = "确认",
     showCancel: Boolean = true,
    onCancel: (() -> Unit)? = null,
    onConfirm: (() -> Unit)? = null,
) {
    AlertDialog(
        this,
        title = title,
        content = content,
        cancelText = cancelText,
        confirmText = confirmText,
        showCancel=showCancel,
        onConfirm = onConfirm,
        onCancel = onCancel
    ).show()
}

@JvmOverloads
fun Fragment.showAlert(
    title: String = "提示",
    content: String? = null,
    cancelText: String = "取消",
    confirmText: String = "确认",
     showCancel: Boolean = true,
    onCancel: (() -> Unit)? = null,
    onConfirm: (() -> Unit)? = null,
) {
    AlertDialog(
        requireActivity(),
        title = title,
        content = content,
        cancelText = cancelText,
        confirmText = confirmText,
        showCancel=showCancel,
        onCancel = onCancel,
        onConfirm = onConfirm
    ).show()
}

private class AlertDialog(
    val activity: Activity,
    var title: String = "提示",
    val content: String? = null,
    var cancelText: String = "取消",
    val confirmText: String = "确认",
    val showCancel: Boolean = true,
    val onCancel: (() -> Unit)? = null,
    val onConfirm: (() -> Unit)? = null
) :
    BaseDialog<DialogAlertBinding>(activity), View.OnClickListener {
    override val widthP: Float
        get() = 0.70F
    override val animation: Int
        get() = R.style.animScaleCenter
    override fun inflaterViewBinding(): DialogAlertBinding {
        return DialogAlertBinding.inflate(LayoutInflater.from(activity))
    }

    override val isCancelable: Boolean
        get() = showCancel
    override val isTouchOutsideDismiss: Boolean
        get() = showCancel

    override fun initialize() {
        binding.tvDgcContent.visibility = if (content == null) View.GONE else View.VISIBLE
        binding.tvDgcCancel.visibility = if (showCancel) View.VISIBLE else View.GONE
        content?.let { binding.tvDgcContent.text = it }
        binding.tvDgcTitle.text = title
        binding.tvDgcCancel.text = cancelText
        binding.tvDgcConfirm.text = confirmText
        binding.tvDgcCancel.setOnClickListener(this)
        binding.tvDgcConfirm.setOnClickListener(this)
        binding.ivClose.setOnClickListener(this)

    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.tv_dgcCancel -> {
                dismiss()
                onCancel?.invoke()
            }
            binding.ivClose.id -> {
                dismiss()
                onCancel?.invoke()
            }
            R.id.tv_dgcConfirm -> {
                dismiss()
                onConfirm?.invoke()
            }
        }
    }
}